<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_chords extends MY_Administrator {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/chords_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();		
		//Load Model
		$this->load->model('administrator_chords_model');
    }

	public function ajax_load_song()
	{
		$song_id = $this->input->post('song_id');
		$dataResult = $this->db->select('t.song_id,t.media_type_id,t.letters,j.song_name,j.song_tag')
		->join('song_info as j','j.song_id=t.song_id','INNER')
		->get_where('song as t',array('t.song_id' => $song_id))
		->row();
		if(!empty($dataResult)):
			$dataArtist = $this->db->select('j.artist_name')
			->join('artist_info as j','j.artist_id=t.artist_id','INNER')
			->get_where('song_artist as t',array('t.song_id' => $song_id,'t.is_default' => '1'))
			->result();
			foreach($dataArtist as $artist):
				$dataResult->artist_name = $artist->artist_name.',';
			endforeach;
			$dataResult->artist_name = @rtrim($dataResult->artist_name,',');
			$dataResult->album_name = $this->db->select('j.album_name')
			->join('album_info as j','j.album_id=t.album_id','INNER')
			->get_where('song_album as t',array('t.song_id' => $song_id,'t.is_default' => '1'),1)
			->row()->album_name;
			
			$dataResult->genre = array();
			$dataGenre = $this->db->order_by('genre_name','ASC')->get('genre')->result();
			foreach($dataGenre as $key => $value):
				$is_exist = $this->db->from('song_genre')
				->where(array('song_id' => $song_id,'genre_id' => $value->genre_id))
				->count_all_results();
				if(!empty($is_exist)):
					$dataResult->genre_key[] = $key;
					$dataResult->genre_name[] = $value->genre_name;				
				endif;
			endforeach;
		else:
			$dataResult = array();
		endif;
		echo json_encode($dataResult);
	}
	
	/*Chords Management*/
	public function content_management($module=NULL,$arg=NULL)
	{
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";
					
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_chords_model->content_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";				
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";
				
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataChordsTable'] = directory_map($this->upload_folder.'../images/chords/chords_table/', 1);
				$this->data['dataGenre'] = $this->db->order_by('genre_name','ASC')->get('genre')->result();				
				
				$this->data['content'] = "administrator/chords/content_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);
				//Check Id exist
				$is_exist = $this->db->from('chords')
				->where(array('chords_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_chords_model->content_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;
				
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";			
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";	
				
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataChordsTable'] = directory_map($this->upload_folder.'../images/chords/chords_table/', 1);
				$this->data['dataGenre'] = $this->db->order_by('genre_name','ASC')->get('genre')->result();			

				$this->data['dataChordsImage'] = $this->db->select('chords_image_id,chords_file_name,chords_file_ext')
				->order_by('chords_image_order ASC,chords_image_id ASC')
				->get_where('chords_image',array('chords_id' => $id))
				->result();					
				$this->data['dataTabsImage'] = $this->db->select('tabs_image_id,tabs_file_name,tabs_file_ext')
				->order_by('tabs_image_order ASC,tabs_image_id ASC')
				->get_where('chords_tabs_image',array('chords_id' => $id))
				->result();
				$this->data['dataResult'] = $this->db
				->join('chords_info as j','j.chords_id=t.chords_id','INNER')
				->get_where('chords as t',array('t.chords_id' => $id),1)->row();
				$dataChordsGenre = $this->db->select('genre_id')->get_where('chords_genre',array('chords_id' => $id))->result();
				$this->data['dataChordsGenre'] = array();
				foreach($dataChordsGenre as $value):
					$this->data['dataChordsGenre'][] = $value->genre_id;
				endforeach;		
	
				$this->data['content'] = "administrator/chords/content_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;
			case 'ajax_delete_chords':
				$id = intval($arg);
				$this->administrator_chords_model->ajax_delete_chords($id);
			break;
			case 'ajax_delete_tabs':
				$id = intval($arg);
				$this->administrator_chords_model->ajax_delete_tabs($id);
			break;
			case 'ajax_delete_album':
				$id = intval($arg);
				$this->administrator_chords_model->ajax_delete_album($id);
			break;	
			case 'ajax_update_chords_image':
				$this->administrator_chords_model->ajax_update_chords_image();			
			break;						
			case 'ajax_update_tabs_image':
				$this->administrator_chords_model->ajax_update_tabs_image();			
			break;				
			case 'delete':
				$this->administrator_chords_model->content_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "t.chords_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'chords_id':
						case 'update_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
					
				/*Search Condition*/
				$searchCondition = array('t.chords_id IS NOT NULL');
				$chords_id = $this->input->get('chords_id');
				$media_type_id = addslashes($this->input->get('media_type_id'));
				$song_name = addslashes($this->input->get('song_name'));
				$artist_name = addslashes($this->input->get('artist_name'));
				$album_name = addslashes($this->input->get('album_name'));
				$update_by = addslashes($this->input->get('update_by'));
				if(!empty($chords_id)) $searchCondition[] = "t.chords_id LIKE '%".$chords_id."%'";
				if(!empty($media_type_id)) $searchCondition[] = "t.media_type_id LIKE '%".$media_type_id."%'";
				if(!empty($song_name)) $searchCondition[] = "j.song_name LIKE '%".$song_name."%'";
				if(!empty($artist_name)) $searchCondition[] = "j.artist_name LIKE '%".$artist_name."%'";
				if(!empty($album_name)) $searchCondition[] = "j.album_name LIKE '%".$album_name."%'";				
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('chords as t')
				->join('chords_info as j','j.chords_id=t.chords_id','INNER')
				->where(''.implode(' AND ',$searchCondition).'')
				->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->order_by($orderCondition)
				->join('chords_info as j','j.chords_id=t.chords_id','INNER')
				->get_where('chords as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
				
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();

				$this->data['content'] = "administrator/chords/content_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}
	
	/*Article Management*/
	public function article_management($module=NULL,$arg=NULL)
	{
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";
					
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_chords_model->article_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				
				//Load Ckeditor
				$this->load->helper('ckeditor');
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";				
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";				
				
				$this->data['content'] = "administrator/chords/article_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);
				//Check Id exist
				$is_exist = $this->db->from('chords_article')
				->where(array('article_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_chords_model->article_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;
				
				//Load Ckeditor
				$this->load->helper('ckeditor');				
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";			
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";
				//Load ZeroClipboard
				$this->data['js'][] = "ZeroClipboard/ZeroClipboard.js";								
				
				$this->data['dataResult'] = $this->db
				->join('chords_article_info as j','j.article_id=t.article_id','INNER')
				->get_where('chords_article as t',array('t.article_id' => $id),1)->row();
				$this->data['dataArticleImage'] = $this->db->order_by('t.article_image_order ASC,t.article_image_id ASC')->get_where('chords_article_image as t',array('t.article_id' => $id))->result();
	
				$this->data['content'] = "administrator/chords/article_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;
			case 'ajax_update_image':
				$this->administrator_chords_model->ajax_article_update_image();			
			break;			
			case 'ajax_delete_image':
				$id = intval($arg);
				$this->administrator_chords_model->ajax_article_delete_image($id);
			break;			
			case 'delete':
				$this->administrator_chords_model->article_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "t.article_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'article_id':
						case 'update_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
					
				/*Search Condition*/
				$searchCondition = array("t.article_type = 'article'");
				$article_id = $this->input->get('article_id');
				$article_subject = addslashes($this->input->get('article_subject'));
				$article_brief = addslashes($this->input->get('article_brief'));
				$update_by = addslashes($this->input->get('update_by'));
				if(!empty($article_id)) $searchCondition[] = "t.article_id LIKE '%".$article_id."%'";
				if(!empty($article_subject)) $searchCondition[] = "j.article_subject LIKE '%".$article_subject."%'";
				if(!empty($article_brief)) $searchCondition[] = "j.article_brief LIKE '%".$article_brief."%'";				
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('chords_article as t')
				->join('chords_article_info as j','j.article_id=t.article_id','INNER')
				->where(''.implode(' AND ',$searchCondition).'')
				->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->order_by($orderCondition)
				->join('chords_article_info as j','j.article_id=t.article_id','INNER')
				->get_where('chords_article as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();

				$this->data['content'] = "administrator/chords/article_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}
	
	/*Clip Management*/
	public function clip_management($module=NULL,$arg=NULL)
	{
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";
					
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_chords_model->clip_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				
				//Load Ckeditor
				$this->load->helper('ckeditor');
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";				
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";				
				
				$this->data['content'] = "administrator/chords/clip_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);
				//Check Id exist
				$is_exist = $this->db->from('chords_article')
				->where(array('article_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_chords_model->clip_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;
				
				//Load Ckeditor
				$this->load->helper('ckeditor');				
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";			
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";				
				
				$this->data['dataResult'] = $this->db
				->join('chords_article_info as j','j.article_id=t.article_id','INNER')
				->get_where('chords_article as t',array('t.article_id' => $id),1)->row();
				$this->data['dataArticleImage'] = $this->db->order_by('t.article_image_order ASC,t.article_image_id ASC')->get_where('chords_article_image as t',array('t.article_id' => $id))->result();
	
				$this->data['content'] = "administrator/chords/clip_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;
			case 'ajax_update_image':
				$this->administrator_chords_model->ajax_clip_update_image();			
			break;				
			case 'ajax_delete_image':
				$id = intval($arg);
				$this->administrator_chords_model->ajax_clip_delete_image($id);
			break;
			case 'ajax_delete_media':
				$id = intval($arg);
				$field = $this->input->get('field');
				$this->administrator_chords_model->ajax_clip_delete_media($id,$field);
			break;								
			case 'delete':
				$this->administrator_chords_model->clip_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;		
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "t.article_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'article_id':
						case 'update_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
					
				/*Search Condition*/
				$searchCondition = array("t.article_type = 'clip'");
				$article_id = $this->input->get('article_id');
				$article_subject = addslashes($this->input->get('article_subject'));
				$article_brief = addslashes($this->input->get('article_brief'));
				$update_by = addslashes($this->input->get('update_by'));
				if(!empty($article_id)) $searchCondition[] = "t.article_id LIKE '%".$article_id."%'";
				if(!empty($article_subject)) $searchCondition[] = "j.article_subject LIKE '%".$article_subject."%'";
				if(!empty($article_brief)) $searchCondition[] = "j.article_brief LIKE '%".$article_brief."%'";				
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('chords_article as t')
				->join('chords_article_info as j','j.article_id=t.article_id','INNER')
				->where(''.implode(' AND ',$searchCondition).'')
				->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->order_by($orderCondition)
				->join('chords_article_info as j','j.article_id=t.article_id','INNER')
				->get_where('chords_article t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();

				$this->data['content'] = "administrator/chords/clip_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}
}